package com.vf.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 地址解析工具类，支持省市区街道等信息提取
 */
public class SplitLocationUtil {
    // 支持完整中文地址提取，匹配省、市、区县、街道
    private static final String ADDRESS_REGEX = "^"
        + "(?<province>[\\u4e00-\\u9fa5]+?(?:省|市|自治区|特别行政区))?"
        + "(?<city>[\\u4e00-\\u9fa5]+?(?:市|地区|自治州|盟))?"
        + "(?<district>[\\u4e00-\\u9fa5]+?(?:区|县|自治县|旗|镇|街道))?"
        + "(?<location>[\\u4e00-\\u9fa5\\d\\s\\S]*)$";


    public static AddressInfo parseAddress(String address) {
        Pattern pattern = Pattern.compile(ADDRESS_REGEX);
        Matcher matcher = pattern.matcher(address);

        if (matcher.find()) {
            String province = matcher.group("province");
            String city = matcher.group("city");
            String district = matcher.group("district");
            String location = matcher.group("location");

            // 如果市为空，则尝试从省中提取市名（如“北京市”）
            if (city == null || city.trim().isEmpty()) {
                if (province != null && province.endsWith("市")) {
                    city = province;
                } else {
                    city = "";
                }
            }

            return new AddressInfo(province, city, district, location);
        } else {
            return new AddressInfo("", "", "", address);
        }
    }

    public static class AddressInfo {
        private final String province;
        private final String city;
        private final String district;
        private final String location;

        public AddressInfo(String province, String city, String district, String location) {
            this.province = province != null ? province : "";
            this.city = city != null ? city : "";
            this.district = district != null ? district : "";
            this.location = location != null ? location : "";
        }

        public String getProvince() {
            return province;
        }

        public String getCity() {
            return city;
        }

        public String getDistrict() {
            return district;
        }

        public String getLocation() {
            return location;
        }

        @Override
        public String toString() {
            return "AddressInfo{" +
                    "province='" + province + '\'' +
                    ", city='" + city + '\'' +
                    ", district='" + district + '\'' +
                    ", location='" + location + '\'' +
                    '}';
        }
    }

    // 示例测试方法
    public static void main(String[] args) {
        String[] addresses = {
                "江西省抚州市黎川县新城大道",
                "北京市朝阳区望京街道",
                "内蒙古自治区呼和浩特市回民区新华东街",
                "香港特别行政区九龙城区",
                "上海市浦东新区世纪大道100号"
        };

        for (String address : addresses) {
            AddressInfo info = parseAddress(address);
            System.out.println("地址: " + address);
            System.out.println(info);
            System.out.println("---");
        }
    }
}